<?php

class Imagen {
  var $strURL      ;
  var $strPath     ;
  
  var $strNombre   ;
  var $strExtension;
  
  var $intW        ;
  var $intH        ;
  
  var $rsImagen;
  
  function Imagen() {
    
  }
  
  function AbrirImagen() {
    switch(strtolower($this->strExtension)) {
      case 'jpg':
        $this->rsImagen = imagecreatefromjpeg($this->strPath);
        break;
      case 'png':
        $this->rsImagen = imagecreatefrompng ($this->strPath);
        break;
      case 'gif':
        $this->rsImagen = imagecreatefromgif ($this->strPath);
        break;
      default:
        echo "NOOOOOOOO" . strtolower($this->strExtension);
    }
  }
  
  function AbrirDesdeURL($strURL) {
    $this->strURL = $strURL;
    global $strGUrl;
    global $strGPath;
    
    if(substr($this->strURL, 0, 7) == "http://") {
      if(substr($strGUrl, 0, 7) == "http://") {
        $strSubCadena1 = "http://" . str_replace('//', '/', substr($this->strURL, 7));
        $strSubCadena2 = "http://" . str_replace('//', '/', substr($strGUrl, 7));
        
        $bolIguales = true;
        for($i = 0; $i < strlen($strSubCadena2); $i++) {
          if($strSubCadena1{$i} != $strSubCadena2{$i}) {
            $bolIguales = false;
            break;
          }
        }
        if(!$bolIguales) {
          echo "NO SE PUEDE CAMBIAR EL PATH DE IMAGEN";
        } else {
          if(substr($strSubCadena2, -1) == '/') {
            $strSubCadena1 = substr($strSubCadena1, strlen($strSubCadena2) - 1);
          } else {
            $strSubCadena1 = substr($strSubCadena1, strlen($strSubCadena2));
          }
        }
        $strSubCadena1 = $strGPath . $strSubCadena1;
        $this->strPath = $strSubCadena1;
      } else {
        for($i = 8; $i < strlen($this->strURL); $i++) {
          if($this->strURL{$i} == "/") {
            break;
          }
        }
        
        $strSubCadena1 = str_replace('//', '/', substr($this->strURL, $i));
        $strSubCadena2 = str_replace('//', '/', $strGUrl);
        
        $bolIguales = true;
        for($i = 0; $i < strlen($strSubCadena2); $i++) {
          if($strSubCadena1{$i} != $strSubCadena2{$i}) {
            $bolIguales = false;
            break;
          }
        }
        if(!$bolIguales) {
          echo "NO SE PUEDE CAMBIAR EL PATH DE IMAGEN";
        } else {
          if(substr($strSubCadena2, -1) == '/') {
            $strSubCadena1 = substr($strSubCadena1, strlen($strSubCadena2) - 1);
          } else {
            $strSubCadena1 = substr($strSubCadena1, strlen($strSubCadena2));
          }
        }
        $strSubCadena1 = $strGPath . $strSubCadena1;
        $this->strPath = $strSubCadena1;
      }
    } else {
      $strSubCadena1 = str_replace('//', '/', substr($this->strURL, $i));
      $strSubCadena2 = str_replace('//', '/', $strGUrl);

      $bolIguales = true;
      for($i = 0; $i < strlen($strSubCadena2); $i++) {
        if($strSubCadena1{$i} != $strSubCadena2{$i}) {
          $bolIguales = false;
          break;
        }
      }
      if(!$bolIguales) {
        echo "NO SE PUEDE CAMBIAR EL PATH DE IMAGEN";
      } else {
        if(substr($strSubCadena2, -1) == '/') {
          $strSubCadena1 = substr($strSubCadena1, strlen($strSubCadena2) - 1);
        } else {
          $strSubCadena1 = substr($strSubCadena1, strlen($strSubCadena2));
        }
      }
      $strSubCadena1 = $strGPath . $strSubCadena1;
      $this->strPath = $strSubCadena1;
    }
    
    $arrInfoSize = getimagesize($this->strPath);
    $arrInfoType = pathinfo    ($this->strPath);
    
    $this->intW  = $arrInfoSize[0];
    $this->intH  = $arrInfoSize[1];
    
    $this->strNombre    = $arrInfoType["basename" ];
    $this->strExtension = $arrInfoType["extension"];
    
    $this->rsImagen = null;
  }
  
  function CropImg($intSrcX, $intSrcY, $intSrcW, $intSrcH, $intDstW, $intDstH, $strNombre = "") {
//    echo "$intSrcX, $intSrcY, $intSrcW, $intSrcH, $intDstW, $intDstH"; die;
    global $strGPath;
    global $strGUploads;
    
    if(!$strNombre) {
      $strNombre = "rs_" . md5(time() . rand()) . ".png";
    } else {
      // Debo suponer que no viene el path completo
    }
    
    @mkdir("$strGPath/$strGUploads/resized");
    @mkdir("$strGPath/$strGUploads/resized/" . $intDstW . "x" . $intDstH);
    
    $strNombre = "$strGPath/$strGUploads/resized/" . $intDstW . "x" . $intDstH . "/" . $strNombre;
    
    $this->AbrirImagen();
    $rsImageDst = imagecreatetruecolor($intDstW, $intDstH);
    imagecopyresampled($rsImageDst, $this->rsImagen, 0, 0, $intSrcX, $intSrcY, $intDstW, $intDstH, $intSrcW, $intSrcH);
    
    imagepng($rsImageDst, $strNombre);
    
    return substr($strNombre, strlen("$strGPath/$strGUploads/resized/"));
  }
  
  function Thumb($intMaxX, $intMaxY = -1, $strTipo = "png", $strSubDir = ".mthumbs/", $bolKeepratio = true) {
    if($intMaxY == -1) {
      $intMaxY = $intMaxX;
    }
    
    $fltPorcientoX = $intMaxX / $this->intW;
    $fltPorcientoY = $intMaxY / $this->intH;
    
    if(($fltPorcientoX >= 1) && ($fltPorcientoY >= 1)) {
      // Ya esta suficientemente pequeña
      return false;
    }
    
    if($bolKeepratio) {
      if($fltPorcientoX < $fltPorcientoY) {
        $fltPorcientoY = $fltPorcientoX;
      } else {
        $fltPorcientoX = $fltPorcientoY;
      }
    }
    
    $intNewX = $fltPorcientoX * $this->intW;
    $intNewY = $fltPorcientoY * $this->intH;
    
    $arrPathInfo = pathinfo($this->strPath);
    $strDir = $arrPathInfo["dirname"] . "/";
    
    @mkdir($strDir . $strSubDir);
    $strNombre = $strDir . $strSubDir . $arrPathInfo["filename"] . "." . $strTipo;
    
    $this->AbrirImagen();
    $rsImageDst = imagecreatetruecolor($intNewX, $intNewY);
    imagecopyresampled($rsImageDst, $this->rsImagen, 0, 0, 0, 0, $intNewX, $intNewY, $this->intW, $this->intH);
    
    switch($strTipo) {
      case 'png':
        imagepng($rsImageDst, $strNombre);
        break;
      case 'jpg':
        imagejpeg($rsImageDst, $strNombre);
        break;
    }
    
    return $arrPathInfo["filename"] . "." . $strTipo;
  }
}
?>
